<?php
session_start();
include_once('config/db_conn.php');
include_once('db_config/db_user.php');
include_once('db_config/db_gigs.php');
include_once('db_config/db_sentmail.php');
include_once('db_config/db_inbox.php');

$sentmail=new sentmail();
$inboxmail=new inbox();
$to=trim($_REQUEST['to'],' ');
$gig_id=$_REQUEST['gig_id'];
$user=new user();

$rs_user=$user->uniq_user_list('user_id',$_SESSION['user_id']);
$data_user=mysql_fetch_array($rs_user);

$rs_user_tomail=$user->uniq_user_list('user_name',$to);
$data_user_tomail=mysql_fetch_array($rs_user_tomail);

$gigs_obj=new gigs();
$rs_gigs=$gigs_obj->uniq_gigs_list('id',$gig_id);
$data_gigs=mysql_fetch_array($rs_gigs);

if($_REQUEST['submit'])
{
	
	
	if($_REQUEST['subject']=='')
	{
		$err_msg_subject="<li>Subject can't be blank.</li>";
		
	}
	
	if($_REQUEST['body']=='')
	{
		$err_msg_body="<li>Body can't be blank.</li>";
		
	}
	if(strlen($_REQUEST['body'])<4)
	{
		$err_msg_short_body="<li>Body is too short (minimum is 4 characters).</li>";
		
	}
	else
	{
	
	
		$data_senmail=array();
	
	
		$data_senmail['gigs_id']=$gig_id;
		$data_senmail['to_id']=$data_user_tomail['user_id'];
		
		$data_senmail['from_id']=$_SESSION['user_id'];
		
		$data_senmail['sub']=$_REQUEST['subject'];
		$data_senmail['body']=$_REQUEST['body'];
		$data_senmail['attachment']=$_FILES['attachment']['name'];
		$data_senmail['date_time']=date('y-m-d h:i:s');
		
	
		$sentmail->dataInsert('ninerr_sentmail',$data_senmail);
	
		$data_inbox['gigs_id']=$gig_id;
		
		$data_inbox['from_id']=$data_user_tomail['user_id'];
		
		
		$data_inbox['to_id']=$_SESSION['user_id'];
		
		
		$data_inbox['sub']=$_REQUEST['subject'];
		$data_inbox['body']=$_REQUEST['body'];
		$data_inbox['attachment']=$_FILES['attachment']['name'];
		$data_inbox['date_time']=date('y-m-d h:i:s');
	
		
		$inboxmail->dataInsert('ninerr_inbox',$data_inbox);
		
		###############################################################
						/*Mail Part */
###############################################################
$to_mail=$data_user_tomail['user_primary_email'];
$from_mail = $data_user['user_primary_email'];
$from_name = $data_user['user_name'];
$subject ="A New message On ".SITE_NAME;

$body = "Sender: ".$data_user['user_name']."\r\n";
$body.= "Message Subject: ".$_REQUEST['subject']."\r\n";
$body.= "Message: ".$_REQUEST['body']."\r\n\r\n";
$body.= "You can goto your inbox and Reply \r\n";
$body.= "<a href='".SITE_URL."messages.php'>Take me to my Inbox</a>\r\n";
$body.= "This mail is sent to you because You are subscribed to ".SITE_NAME."\r\n";

$headers  = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers.="To: ".$to_mail."\r\n";
$headers.="From: ".$from_name." <".$from_mail.">\r\n";

@mail($to_mail, $subject, nl2br($body), $headers);



###############################################################

		reDirect('messages.php');
		
	}	
}			
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title><?php echo SITE_TITLE ;?></title>
<link href="css/style.css" rel="stylesheet" type="text/css" />
<link href="css/custom.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="javascript/jquery.min.js"></script>
<script type="text/javascript" src="javascript/jquery.simplemodal.js"></script>
<script type="text/javascript" src="javascript/init.js"></script>
<script type="text/javascript" src="javascript/init1.js"></script>
</head>
<body >
<div id="container" class="content">

<!--HEADER AREA-->
<?php include('header.php')?>
<!--HEADER AREA END-->
<div id="main" class="clearfix">
	<div class="main-tl">
	  	<div class="main-tr"></div>
	  	<div class="main-top"></div>
	</div>
	<div class="main-inner clearfix">
		<div class="side1">
		<!-- side1 content BEGIN =============================================================================================== -->
	<h2 class="title"><span style="color: #2791B9;">Inbox /</span> Compose</h2>
	<form method="post" id="MessageComposeForm" enctype="multipart/form-data"  action="" class="compose normal">
		<div class="message-block clearfix">
			<div class="message-block-left">
				<p>Compose a new message to <?php echo $to;?> user</a></p>
			</div>
			<div class="message-block-right">
				<div class="message-show-right-block inbox-option">
					<span class="select">Show:</span>
							<span><a title="All Messages" class="" href="messages.php?mailbox=inbox">Inbox</a>, </span>
							<span><a title="Sent Items" class="" href="messages.php?mailbox=sent">Sent Items</a> </span>
				</div>
			</div>
		</div>
		<span class="message-title">From: </span><?php echo $data_user['user_name']; ?><br />
		<span class="message-title">To: </span><a href="users_<?php echo $to;?>_gigs_<?php  echo str_replace(' ','-', $data_gigs['gigs_title']);?>"><?php echo $to;?></a><br />	
		<span class="message-title">Gis: </span><a href="users_<?php echo $to;?>_gigs_<?php  echo str_replace(' ','-', $data_gigs['gigs_title']);?>"><?php echo $data_gigs['gigs_title']; ?></a><br />
		
		<!-- compose content BEGIN -->
		<div class="compose-box">
			<div class="input text required">
				<label>Subject</label>
				<input type="text" value="<?php echo $_REQUEST['subject']; ?>" name="subject" />
				<?php 
				if($err_msg_subject)
				{ ?>
				<div class="error-message"><?php echo $err_msg_subject; ?></div>
					<?php 
				}?>
			</div>
			<div class="input required gig-img-label message-lable-info">
				<label>Message</label>
			</div>
			<div class="input textarea required">
				<label></label>
				<textarea rows="6" cols="30" name="body"><?php echo $_REQUEST['body']; ?></textarea>
				<?php 
				if($err_msg_body || $err_msg_short_body)
				{ ?>
				<div class="error-message"><?php echo $err_msg_body; ?><?php echo $err_msg_short_body ?></div>
					<?php 
				}?>	
			</div>			
			<span class="message-lable-info">Attachment	</span>
            <div class="atachment">
				<div class="input file">
					<label for="AttachmentFilename"></label>
					<input type="file" value="" class="multi file attachment browse-field" size="33" name="attachment" />
					<p style="margin:10px 0 0 165px">Attachment <em>maximum 50 Mb</em></p>
				</div>		
			</div>
			<div class="compose-block clearfix">
				<div class="message-block-right clearfix">
					<div class="submit"><input type="submit" value="Send" name="submit" /></div>		
					<div class="cancel-block">
	    				<a title="Cancel" href="messages.php?mailbox=inbox">Cancel</a>    
					</div>
				</div>
			</div>
		</div>
		<!-- compose content END -->
	</form>	
		<!-- side1 content END ============================================================================================ -->
		</div><!-- end side1 -->
		<!-- side2 BEGIN ====================== -->
		<?php include('right.php'); ?>
		<!-- side2 END ====================== -->
	</div><!-- end main-inner-->
</div><!-- end main -->

<!--FOOTER AREA-->
<?php include('footer.php');?>
 <!--FOOTER AREA END-->

</div><!-- end container -->
</body>
</html>